package dk.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ReturnData.ResponseData;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import dk.Utils.COSUtils;
import dk.Utils.POIUtils;
import dk.beans.Examlist;
import dk.beans.subject;
import dk.beans.EasyPoiExcelBean.OptionExamlist;
import dk.beans.EasyPoiExcelBean.ShortAnswerExamlist;
import dk.mapper.examlistmapper;
import dk.mapper.subjectmapper;

@RestController
public class FileController {
	@Autowired
	examlistmapper examlistmapper;
	@Autowired
	subjectmapper subjectmapper;
	//本地文件缓存路径
	final String  fileurl = "C:\\Users\\34227\\Desktop\\实习笔记\\easypoi文件\\";
	//cos前缀路径
	final String  url = "https://misaka917-1301164487.cos.ap-nanjing.myqcloud.com/";
//得到单选下载链接返回给前端
@RequestMapping("/CreateOptionExampleExcel")
public ResponseData CreateOptionExcel() throws IOException {
	ResponseData data = new ResponseData();
	
	String path="C:\\Users\\34227\\Desktop\\实习笔记\\easypoi文件\\单选.xls";
	String CloudPath = "DK-powerful//example//excel//单选.xls";
	String TableName="请录入单选数据";
	String SheetName = "单选模组";
	Class<OptionExamlist> clazz =OptionExamlist.class;
	List<OptionExamlist> list = new ArrayList<OptionExamlist>();
	list.add(new OptionExamlist());

	POIUtils.WriteToFile(path, TableName, SheetName, clazz, list);
	
	//把这个文件发送给cos云存储 这是应返回链接
	COSUtils.uploadFileByPath(path, CloudPath);
	data.setData("https://misaka917-1301164487.cos.ap-nanjing.myqcloud.com/"+CloudPath);
	data.setCode(200);
	data.setMessage("获取单选模板链接成功");
	return data;
}
//得到简答的下载链接返回给前端
@RequestMapping("/CreateShortAnswerExamlist")
public ResponseData CreateShortAnswerExamlist() throws IOException {
	ResponseData data = new ResponseData();
	String path="C:\\Users\\34227\\Desktop\\实习笔记\\easypoi文件\\简答模板.xls";
	String CloudPath = "DK-powerful//example//excel//简答.xls";
	String TableName="请录入简答数据";
	String SheetName = "简答模组";
	Class<ShortAnswerExamlist> clazz = ShortAnswerExamlist.class;
	List<ShortAnswerExamlist> list = new ArrayList<ShortAnswerExamlist>();
	list.add(new ShortAnswerExamlist());
    POIUtils.WriteToFile(path, TableName, SheetName, clazz, list);
	
	//把这个文件发送给cos云存储 这是应返回链接
	COSUtils.uploadFileByPath(path, CloudPath);
	data.setData(url+CloudPath);
	data.setCode(200);
	data.setMessage("获取简答模板链接成功");
	return data;
	
	
}
//得到全部数据的excel并上传到cos 且提供返回的下载链接
@RequestMapping("GetExamListToExcel")
public ResponseData GetExamListToExcel() throws IOException {
	ResponseData data = new ResponseData();
	String path="C:\\Users\\34227\\Desktop\\实习笔记\\easypoi文件\\全部题目.xls";
	String CloudPath = "DK-powerful//example//excel//全部题目.xls";
	
	String TableName="试题信息";
	String SheetName = "试题信息导出";
	List<Examlist> list = examlistmapper.IselectexamList();
	Class<Examlist> clazz = Examlist.class;
	for(Examlist examlist : list) {
		System.out.println(examlist);
	}
	//写进excel
    POIUtils.WriteToFile(path, TableName, SheetName, clazz, list);
    COSUtils.uploadFileByPath(path, CloudPath);
	
	data.setCode(200);
	data.setMessage("全部试题导出excel成功");
	data.setData(url+CloudPath);
	
	return data;
}
//得到全部学科数据写入excel上传到cos返回链接
@RequestMapping("GetSubjectListToExcel")
public ResponseData GetSubjectListToExcel() throws IOException {
	String path="C:\\Users\\34227\\Desktop\\实习笔记\\easypoi文件\\全部学科.xls";
	String CloudPath = "DK-powerful//example//excel//全部学科.xls";
	
	String TableName="学科信息";
	String SheetName = "蟹壳信息导出";
	ResponseData data =new ResponseData();
	List<subject> list = subjectmapper.IselectSubjectList();
	Class<subject> clazz = subject.class;
	data.setCode(200);
	POIUtils.WriteToFile(path, TableName, SheetName, clazz, list);
	//上传到cos
	COSUtils.uploadFileByPath(path, CloudPath);
	data.setMessage("全部学科导出成功");
	data.setData(url+CloudPath);
	return data;
}
//文件上传 之后解析 返回数据
@RequestMapping("/GetSubjectListByExcel")
public ResponseData  GetSubjectListByExcel(MultipartFile file) throws IOException {
	
	//把file放到本低
	File localfile = new File(fileurl+"上传学科列表.xls");
	FileUtils.copyInputStreamToFile(file.getInputStream(), localfile);
	//把他解析出来获取文件
	
	ResponseData data = new ResponseData();
	ImportParams importParams = new ImportParams();
	importParams.setTitleRows(1);
	importParams.setHeadRows(1);
	List<subject> list = ExcelImportUtil.importExcel(localfile, subject.class,importParams);
	for(subject subject:list) {
		System.out.println(subject);
		subject.setCreatetime(null);
		subject.setUpdatetime(null);
		//subjectmapper.insert(subject);
	}
	data.setCode(200);
	data.setMessage("学科信息解析成功");
	data.setData(list);
	return data;
	
}
//单选试题      文件上传 之后解析 返回的是list列表  需要在cos上生成对应的文件夹
@PostMapping("GetOptionExamListByExcel")
public ResponseData GetExamListByExcel(MultipartFile file) throws IOException {
	ResponseData data = new ResponseData();

	//把file放到本低
	File localfile = new File(fileurl+"上传单选试题列表.xls");
	FileUtils.copyInputStreamToFile(file.getInputStream(), localfile);
	//把他解析出来获取文件
	ImportParams importParams = new ImportParams();
	importParams.setTitleRows(1);
	importParams.setHeadRows(1);
	List<Examlist> list = ExcelImportUtil.importExcel(localfile, Examlist.class,importParams);
	for(Examlist examlist:list) {
		//因为update和create不可见但是写入进去了 所以要置为空
		examlist.setId(null);
		examlist.setCreatetime(null);
		examlist.setCreatetime(null);
		System.out.println(examlist);
		//examlistmapper.insert(examlist);
	}
	data.setCode(200);
	data.setMessage("单选试题解析成功");
	data.setData(list);
    return data;
}
//单选试题      文件上传 之后解析 返回的是list列表  需要在cos上生成对应的文件夹
@PostMapping("GetShortAnswerExamListByExcel")
public ResponseData GetShortAnswerExamListByExcel(MultipartFile file) throws IOException{
	ResponseData data = new ResponseData();

	//把file放到本低
	File localfile = new File(fileurl+"上传简答试题列表.xls");
	FileUtils.copyInputStreamToFile(file.getInputStream(), localfile);
	//把他解析出来获取文件
	ImportParams importParams = new ImportParams();
	importParams.setTitleRows(1);
	importParams.setHeadRows(1);
	List<Examlist> list = ExcelImportUtil.importExcel(localfile, Examlist.class,importParams);
	for(Examlist examlist:list) {
		//因为update和create不可见但是写入进去了 所以要置为空
		examlist.setId(null);
		examlist.setCreatetime(null);
		examlist.setCreatetime(null);
		System.out.println(examlist);
		//examlistmapper.insert(examlist);
	}
	data.setCode(200);
	data.setMessage("简答试题解析成功");
	data.setData(list);
    return data;
}
//

}
